<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Constructor Injection</title></head>


<body>
<h2>Overview</h2>

<p>Constructor Injection is a <a href="injection.html" title="Dependency Injection">Dependency Injection</a> variant
    where a component gets all its dependencies via its constructor.</p>

<p>The most important benefits of Constructor Injection are:</p>
<ul>
    <li>It makes a strong dependency contract</li>
    <li>It makes testing easy, since dependencies can be passed in as <a href="mock-objects.html" title="Mock Objects">Mock
        Objects</a></li>
    <li>It is very succinct in terms of lines of code</li>
    <li>Classes that rely on Constructor Injection are generally <a href="patterns.html" title="Good Citizen">Good
        Citizens</a></li>
  <li>A dependency may be made immutable by making the dependency reference <em>final</em></li>
</ul>
<p><img class="emoticon" src="http://docs.codehaus.org/images/icons/emoticons/information.gif" alt="" align="middle"
        border="0" height="16" width="16"/> Martin Fowler explains <a
        href="http://www.martinfowler.com/articles/injection.html#ConstructorInjectionWithPicocontainer">Constructor
    Injection</a>
    in more detail.<br/>
</p>

<h2>Origin</h2>
<p>In 2003, <a href="http://www.twelve71.com/rachel/">Rachel Davies</a>, was reviewing  book <a href="http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471463620.html">Java Open Source Programming</a> for  Joe Walnes.
    She left a Fermat-like margin note when looking at a code sample that used
    Setter Injection: "Why not use constructors ?". Brilliant and simple.
    &nbsp;Using constructors per se, is an ordinary OO feature, but having
    a container that works out what to inject where amongst a list of
constructor arguments is something that is useful too. Read more about the <a href="inversion-of-control-history.html">back history</a>.</p>
<h2>Example</h2>

<div class="source">
            <pre>
public class Apple {
    private final Orange orange;
    private final Pear pear;
    private final Banana banana;
	
    public Apple(Orange orange, Pear pear, Banana banana) {
        this.orange = orange;
        this.pear = pear;
        this.banana = banana;
    }
	
    // methods
}</pre>
</div>
<p>Note, for this there is no need to declare needs in any other way. No interfaces, no doclet tags, no external
    XML. Just your simple component(s) and PicoContainer. No need for post assembly/config initialization either. If
    it is constructed (not withstanding some asserts on nulls) it has its needs satisfied. Components need not be
    interface/implementation separated. This is the coder's choice.</p>

<h3>Using Constructor
    Injector Components
    Without a Container.</h3>

<p>The component can be used directly, without any container. The missing dependency scenario is not an issue
    since it is impossible to instantiate an object without all dependencies being satisfied.</p>

<div class="source">
    <div>
        <pre>Apple apple = new Apple(myOrange, aPear, theBanana);</pre>
    </div>
</div>
<h3>Using Constructor
    Injector Components
With a PicoContainer</h3>
<div class="source">
  <pre>pico = new DefaultPicoContainer(new ConstructorInjection());
pico.addComponent(Apple.class);
// etc
Apple apple = pico.getComponent(Apple.class);</pre>
</div>
<p>Constructor Injection, is a default too (via AdaptiveInjection):</p>
<div class="source">
  <pre>pico = new DefaultPicoContainer();
pico.addComponent(Apple.class);
// etc
Apple apple = pico.getComponent(Apple.class);</pre>
</div>
<p> The PicoContainer team recommends Constructor  Injection over other types - the project was started to pioneer this
  approach. With PicoContainer is no need
  to mark up the constructor with an annotation. Having more than one
  constructor is OK too, as PicoContainer will try to use the one with
  the most arguments and fall back to ones with fewer if it cannot
  satisfy the longer ones.</p>
<p>The component factory for this is <strong>ConstructorInjection</strong>.
  It only handles constructor injection types of components. Factory <strong>AdaptiveInjection</strong> defaults to constructor injection, after checking first to see it the
  component in question is an Annotated Method or Field type (see below).</p>
<p class="callout">Constructor Injection is idiomatic
  PicoContainer
  - choose this style over all others if you can - especially if you are
  starting out with Dependency Injection.</p>
<h2>Summary
</h2>
<p>PicoContainer was the first lightweight container to support and popularize this for of dependency injection. PicoContainer  also supports <a href="setter-injection.html"
                                                                            title="Setter Injection">Setter
Injection</a> that is idiomatic of  the Spring Framework.</p>
</body>
</html>